﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Data;
using GSSoftSL.Utility;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Windows.Data.DomainServices;

namespace GSSoftSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class CaHocViewModel : ViewModelBase
    {
        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }
        private GSSoftContext _dc;

        /// <summary>
        /// Initializes a new instance of the CaHocViewModel class.
        /// </summary>
        public CaHocViewModel()
        {
            Init();
        }

        private void Init()
        {
            _dc = new GSSoftContext();
            IsBusy = true;
            _dc.Load<CaHoc>(_dc.GetCaHocsQuery(), LoadBehavior.RefreshCurrent, LoadCaHoc_Completed, null);
        }

        private void LoadCaHoc_Completed(LoadOperation<CaHoc> loadCaHoc)
        {
            CaHocs = new PagedCollectionView(new EntityList<CaHoc>(_dc.CaHocs, loadCaHoc.Entities));
            IsBusy = false;
        }

        private PagedCollectionView _caHocs;
        public PagedCollectionView CaHocs
        {
            get { return _caHocs; }
            set
            {
                _caHocs = value;
                RaisePropertyChanged("CaHocs");
            }
        }

        private CaHoc _current;
        public CaHoc Current
        {
            get { return _current; }
            set
            {
                if (_current == value)
                    return;
                _current = value;
                if (_current != null && _current.MaCaHoc == null)
                    _current.MaCaHoc = "";
                RaisePropertyChanged("Current");
            }
        }

        private RelayCommand<object> _saveCommand;
        public RelayCommand<object> SaveCommand
        {
            get
            {
                return _saveCommand
                       ?? (_saveCommand = new RelayCommand<object>(
                                              o =>
                                                  {
                                                      IsBusy = true;
                                                      /*IsValidMaCaHoc();
                                                      while (IsBusy)
                                                      {
                                                          
                                                      }*/
                                                      if (Current == null || !_dc.HasChanges || _dc.IsSubmitting)
                                                          return;
                                                      ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                                  },
                                              o => !(Current == null || !_dc.HasChanges || _dc.IsSubmitting)));
            }
        }

        private void Save()
        {
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            IsBusy = true;
            _dc.SubmitChanges(SubmitChangesEnd, null);
        }

        private bool IsValidMaCaHoc()
        {
            //_dc.GetCaHocsQuery().Where(n => n.MaCaHoc == _current.MaCaHoc)
            IsBusy = false;
            return true;
        }

        private void SubmitChangesEnd(SubmitOperation obj)
        {
            if (obj.HasError)
            {
                //obj.Error
            }
            IsBusy = false;
        }

        private RelayCommand _refreshCommand;
        public RelayCommand RefreshCommand
        {
            get
            {
                return _refreshCommand
                    ?? (_refreshCommand = new RelayCommand(Init));
            }
        }

    }
}